package com.dapeis.core.utils.json;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.Map;

import com.ag.utils.DateUtils;
import com.ag.utils.SpringUtils;
import com.ag.utils.json.AGsonBuilder;
import com.dapeis.core.utils.DBConfigUtils;
import com.dapeis.core.utils.StringUtil;
import com.dapeis.domain.UserInformation;
import com.dapeis.service.StylistCityService;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
 * 
 * Title:UsreInfoGsonAdapter.java UsreInfoGsonAdapter Description:
 * 
 * @author yuanyong
 * @date 2017年5月10日 下午3:58:49 Email: hzyuanyong@126.com
 *
 */
public class UserInfoGsonAdapter implements JsonSerializer<UserInformation> {

    @Override
    public JsonElement serialize(UserInformation src, Type typeOfSrc,
	    JsonSerializationContext context) {
	JsonObject json = new JsonObject();
	json.addProperty("sex", src.getSex());
	json.addProperty("faceshpe", src.getFaceshpe());
	json.addProperty("colorofskin", src.getColorofskin());
	json.addProperty("bodySize", src.getBodySize());
	json.add(
		"problems",
		AGsonBuilder.create().toJsonTree(
			DBConfigUtils.paserUserInfoStrArray(src.getProblem())));
	json.add(
		"styles",
		AGsonBuilder.create().toJsonTree(
			DBConfigUtils.paserUserInfoStrArray(src.getStyle())));
	json.addProperty("weight", src.getWeight());
	json.addProperty("heigh", src.getHeigh());
	json.addProperty("chest", src.getChest());
	json.addProperty("waist", src.getWaist());
	json.addProperty("hip", src.getHip());
	json.addProperty("professional", StringUtil.isNotBlank(src
		.getProfessional()) ? src.getProfessional() : new String());
	json.addProperty("cityName",
		DBConfigUtils.getCityName(src.getCityCode()));
	Map map = getCityCode(src.getCityCode());

	json.addProperty(
		"provinceCode",
		null == map.get("provinceCode") ? new String() : map.get(
			"provinceCode").toString());
	json.addProperty("cityCode", null == map.get("cityCode") ? new String()
		: map.get("cityCode").toString());
	json.addProperty(
		"countyCode",
		null == map.get("countyCode") ? new String() : map.get(
			"countyCode").toString());
	json.addProperty(
		"birthday",
		null == src.getBirthday() ? new String() : DateUtils.format(
			DateUtils.YYYY_MM_DD,
			new Date(src.getBirthday() * 1000)));
	json.addProperty("fullCityName",
		DBConfigUtils.getFullCityName(src.getCityCode()));
	json.addProperty("age",
		DBConfigUtils.getAgeByBirthday(src.getBirthday()));
	json.add(
		"lifeImgs",
		AGsonBuilder.create()
			.toJsonTree(
				DBConfigUtils.paserRequirementLifeImg(src
					.getLifeImg())));
	return json;
    }

    public Map getCityCode(String code) {
	StylistCityService cityService = SpringUtils
		.getBean(StylistCityService.class);
	return cityService.getProvince_City_CountyCode(code);
    }

}
